package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private MenuService menuService;

    @PostMapping("/findPage")
    @PreAuthorize("hasAuthority('MENU_QUERY')")//权限控制
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult<Menu> pageResult = menuService.findPage(queryPageBean);
        return new Result(true, "查询菜单成功", pageResult);
    }

    @PostMapping("/add")
    @PreAuthorize("hasAuthority('MENU_ADD')")
    public Result add(@RequestBody Menu menu){
        menuService.add(menu);
        return  new Result(true, "增加菜单项成功");
    }

    @GetMapping("/findById")
    @PreAuthorize("hasAuthority('MENU_QUERY')")
    public Result findById(int id){
        Menu menu = menuService.findById(id);
        return new Result(true, "查询菜单成功", menu);
    }

    @PostMapping("/update")
    @PreAuthorize("hasAuthority('MENU_EDIT')")
    public Result update(@RequestBody Menu menu){
        menuService.update(menu);
        return  new Result(true, "编辑菜单成功");
    }

    @PostMapping("/deleteById")
    @PreAuthorize("hasAuthority('MENU_DELETE')")
    public Result deleteById(int id){
        menuService.deleteById(id);
        return new Result(true, "删除菜单成功");
    }
}
